clear
set more off 
set matsize 11000
cap log close
estimates clear

#delimit ;

*****************************************************************************************************************;
*Paths;
*****************************************************************************************************************;

*access files in subdirectory generated by data creating dofiles;
local arsenicdataset "dtafiles/HouseholdArsenic_2004.dta";

*generate subdirectory to store csv files;
cap mkdir csvfiles;

*****************************************************************************************************************;
*LOCALS;
*****************************************************************************************************************;

local table1=1;
local table2=1;

local tablename1 "csvfiles/Table1householdmeasures";
local tablename2 "csvfiles/Table2predictingswitching";

*****************************************************************************************************************;
*DATA FINALIZATION;
*****************************************************************************************************************;

use "`arsenicdataset'", clear;

*****************************************************************************************************************;
*TABLES;
*****************************************************************************************************************;

**Table 1: Household Arsenic Measures**;

if `table1'==1 {;

// varlist contains column variables;
local varlist "heardofarsenic numarsenictest contaminatedwell redwell paintedwell"; 

local replace replace;
foreach indepvar of varlist wfraction_mindistanceC_1m_G
	fraction_mindistanceC_1m_G fractioncont5 numcontaminated5 as_walkable5_g {;

	local i=1;
	foreach var of varlist `varlist' {;
		reg `var' `indepvar', cluster(dhsid);
		est sto x`i';
		local i=`i'+1;
	};
	esttab _all using "`tablename1'.csv", se b `replace' nocons noobs nonotes 
		nonumbers nodepvars nolines noeqlines compres star(* 0.10 ** 0.05 *** 0.01);
	local replace append;
	estimates clear;
};

*Row N - this just gives us means;
local i=1;
foreach var of varlist `varlist' {;
	reg `var' ;
	est sto x`i';
	local i=`i'+1;
};
esttab _all using "`tablename1'.csv", not b append noobs
	nonumbers nodepvars nolines noeqlines compres nostar;
estimates clear;
};

*End(Table1 Household Arsenic Measures)*/; 
************************************************************************************************************************;

if `table2'==1 {;
** Table 2: Predicting Switching;

local clean1="fraction_mindistanceU_1m_G"; *-;
local clean2="wfraction_mindistanceU_1m_G"; *-;
local clean3="mean_mindistanceU_G"; *+;
local clean4="wmean_mindistanceU_G"; *+;

local contamination="wfraction_mindistanceC_1m_G";
local depvar="contaminatedorsurface";

	local i=1;
	
	forvalues j=1(1)4 {;
		gen interaction=`contamination'*`clean`j'';

		areg `depvar' `contamination' `clean`j'' interaction, absorb(shdistr) cluster(dhsid);
		est sto x`i';
		local i=`i'+1;
		drop interaction;
	};

	esttab _all using "`tablename2'.csv", se stats(r2 N) b replace nocons noobs nonotes 
		nonumbers nolines noeqlines compres star(* 0.10 ** 0.05 *** 0.01);
	estimates clear;
};		

*End(Table2 Predicting Switching)*/; 
************************************************************************************************************************;




